<template>
  <main-page>
    <div slot="body">
      <el-form :inline="true" :model="search" size="mini">
        <el-form-item>
          <el-button type="primary" @click="dialogFormVisible = true;">新增</el-button>
        </el-form-item>
      </el-form>

      <el-table :data="tableData" style="width: 100%" tooltip-effect="dark">
        <el-table-column prop="id" label="编号" width="80"></el-table-column>
        <el-table-column prop="productName" label="产品名称"></el-table-column>
        <el-table-column label="产品单价">
          <template slot-scope="scope">￥ {{scope.row.price}} 元</template>
        </el-table-column>
        <el-table-column label="产品价值">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.productType == 1">{{scope.row.obtain}} 金币</el-tag>
            <el-tag v-else-if="scope.row.productType == 2">{{scope.row.obtain}} 天</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="额外价值">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.productType == 1">{{scope.row.extraObtain}} 金币</el-tag>
            <el-tag v-else-if="scope.row.productType == 2">{{scope.row.extraObtain}} 天</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="产品类型">
          <template slot-scope="scope">
            <el-tag type="info" v-if="scope.row.productType == 1">GOLD</el-tag>
            <el-tag type="info" v-else-if="scope.row.productType == 2">VIP</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="是否最热">
          <template slot-scope="scope">
            <el-tooltip :content="scope.row.isHot == 0 ? '否' : '是'" placement="top">
              <el-switch
                v-model="scope.row.isHot"
                active-color="#13ce66"
                inactive-color="#ff4949"
                :active-value="1"
                :inactive-value="0"
                @change="updateHot(scope.row)"
              ></el-switch>
            </el-tooltip>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
              type="primary"
              icon="el-icon-edit"
              circle
              title="编辑"
              size="small"
              @click="handleClick2EditById(scope.row)"
            ></el-button>
            <el-button
              type="danger"
              icon="el-icon-delete"
              circle
              title="删除"
              size="small"
              @click="handleClick2DeleteById(scope.row.id)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination :listNum="listNum" @currentChange="handleCurrentChange"></pagination>

      <el-dialog title="字典管理" :visible.sync="dialogFormVisible" width="40%">
        <el-form ref="form" :model="form" :rules="rules" label-width="80px">
          <el-form-item label="产品类型" prop="productType">
            <el-radio-group v-model="form.productType">
              <el-radio :label="1">GOLD</el-radio>
              <el-radio :label="2">VIP</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="产品名称" prop="productName">
            <el-input v-model="form.productName" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="产品价值" prop="obtain">
            <el-input-number v-model="form.obtain" :min="0"></el-input-number>
          </el-form-item>
          <el-form-item label="额外价值" prop="extraObtain">
            <el-input-number v-model="form.extraObtain" :min="0"></el-input-number>
          </el-form-item>
          <el-form-item label="产品单价" prop="price">
            <el-input-number v-model="form.price" :min="1"></el-input-number>
          </el-form-item>
          <el-form-item label="是否最热" prop="isHot">
            <el-radio-group v-model="form.isHot">
              <el-radio :label="1">是</el-radio>
              <el-radio :label="0">否</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="onSubmit">提交</el-button>
        </div>
      </el-dialog>
    </div>
  </main-page>
</template>

<script>
import {
  getProductList,
  saveProduct,
  deleteProduct,
  updateProductById
} from "api/ProductApi";

export default {
  name: "ProductList",
  data() {
    return {
      search: {
        currentIndex: 1
      },
      form: {
        id: "",
        productName: "",
        obtain: 0,
        extraObtain: 0,
        price: 0,
        isHot: 0,
        productType: 1
      },
      rules: {
        productName: [{required: true, message: "请输入产品名称"}],
        obtain: [{required: true, message: "请输入产品价值"}],
        extraObtain: [{required: true, message: "请输入额外价值"}],
        price: [{required: true, message: "请输入产品单价"}],
        productType: [{required: true, message: "请选择产品类型"}],
        isHot:[{required: true, message: "请选择是否最热"}]
      },
      tableData: [],
      listNum: 1,
      dialogFormVisible: false
    };
  },
  methods: {
    getProductList() {
      getProductList(this.search).then(res => {
        this.tableData = res.datas;
        this.listNum = res.pagination.totalCount;
      });
    },
    handleCurrentChange(page) {
      this.search.currentIndex = page;
      this.getProductList();
    },
    onSubmit() {
      this.$refs.form.validate(valid => {
        if (valid) {
          if (this.form.id) {
            //修改
            updateProductById(this.form).then(res => {
              if (res) {
                this.$base.showOk();
                this.dialogFormVisible = false;
                this.getProductList();
              }
            });
          } else {
            //保存
            saveProduct(this.form).then(res => {
              if (res) {
                this.$base.showOk();
                this.dialogFormVisible = false;
                this.getProductList();
              }
            });
          }
        }
      });
    },
    handleClick2EditById(row) {
      this.dialogFormVisible = true;
      this.$base.set2Form(this.form, row);
    },
    handleClick2DeleteById(id) {
      this.$base.deleteConfirm(() => {
        deleteProduct({ id }).then(res => {
          if (res) {
            this.$base.deleteOk();
            this.getProductList();
          }
        });
      });
    },
    updateHot(row) {
      updateProductById({
        id: row.id,
        isHot: row.isHot
      }).then(res => {
        if (res) {
          this.$base.showOk();
          this.dialogFormVisible = false;
          this.getProductList();
        }
      });
    }
  },
  created() {
    this.getProductList();
  },
  watch: {
    dialogFormVisible(newDialogFormVisible) {
      if (!newDialogFormVisible) {
        this.$base.clearObj(this.form);
        this.$refs.form.resetFields();
      }
    }
  }
};
</script>

<style lang="stylus" scoped></style>